/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InviteUser.java
 *
 * Created on 27 Apr, 2009, 12:42:45 PM
 */

package gclient;

import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author sv
 */
public class InviteUser extends javax.swing.JDialog {

    /** Creates new form InviteUser */
    public InviteUser(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        parentFrame = (gclient.GalaxiaClientUI)this.getOwner();

        initComponents();
        dlmUserList = new javax.swing.DefaultListModel();
        dlmSelectedUsers = new javax.swing.DefaultListModel();
        lstUserList.setModel(dlmUserList);
        lstSelectedUsers.setModel(dlmSelectedUsers);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstUserList = new javax.swing.JList();
        btnInvite = new javax.swing.JButton();
        btnRefreshUserList = new javax.swing.JButton();
        btnSendInvite = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSelectedUsers = new javax.swing.JList();
        btnRemove = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Invite User(s)");

        lstUserList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstUserList);

        btnInvite.setText("Invite >>");
        btnInvite.setEnabled(false);
        btnInvite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInviteActionPerformed(evt);
            }
        });

        btnRefreshUserList.setText("Refresh User List");
        btnRefreshUserList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshUserListActionPerformed(evt);
            }
        });

        btnSendInvite.setText("Send Invitation");
        btnSendInvite.setEnabled(false);
        btnSendInvite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendInviteActionPerformed(evt);
            }
        });

        lstSelectedUsers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lstSelectedUsers);

        btnRemove.setText("Remove <<");
        btnRemove.setEnabled(false);
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnRefreshUserList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnInvite, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRemove))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnSendInvite, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnRefreshUserList)
                            .addComponent(btnSendInvite, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(btnInvite)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRemove)))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendInviteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendInviteActionPerformed
        // TODO add your handling code here:
        for(int i = 0; i < dlmSelectedUsers.size(); ++i) {
            try {
                parentFrame.mainTracker.sendInvite(parentFrame.getSelectedGroup(), (String) dlmSelectedUsers.get(i));
            } catch (Exception iOException) {}
        }
        parentFrame.setUserInvited(true);
        setVisible(false);
}//GEN-LAST:event_btnSendInviteActionPerformed

    private void btnRefreshUserListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshUserListActionPerformed
        // TODO add your handling code here:
        try {
            Vector<String> foo = parentFrame.mainTracker.getUserList(parentFrame.getSelectedGroup());
            dlmUserList.clear();
            for(String s : foo) dlmUserList.addElement(s);
            if(!dlmUserList.isEmpty()) btnInvite.setEnabled(true);
            else btnInvite.setEnabled(false);
        } catch (IOException e) {
            System.err.println(e);
        }
    }//GEN-LAST:event_btnRefreshUserListActionPerformed

    private void btnInviteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInviteActionPerformed
        // TODO add your handling code here:
        dlmSelectedUsers.addElement(dlmUserList.elementAt(lstUserList.getSelectedIndex()));
        if(!btnRemove.isEnabled()) btnRemove.setEnabled(true);
        if(!btnSendInvite.isEnabled()) btnSendInvite.setEnabled(true);
    }//GEN-LAST:event_btnInviteActionPerformed

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        // TODO add your handling code here:
        dlmSelectedUsers.removeElementAt(lstSelectedUsers.getSelectedIndex());
        if(dlmSelectedUsers.isEmpty()) {
            btnRemove.setEnabled(false);
            btnSendInvite.setEnabled(false);
        }
    }//GEN-LAST:event_btnRemoveActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                InviteUser dialog = new InviteUser(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInvite;
    private javax.swing.JButton btnRefreshUserList;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnSendInvite;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstSelectedUsers;
    private javax.swing.JList lstUserList;
    // End of variables declaration//GEN-END:variables

    protected javax.swing.DefaultListModel dlmUserList, dlmSelectedUsers;
    protected GalaxiaClientUI parentFrame;
}
